//
//  GYDSettingViewController.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/26.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import SnapKit

class GYDSettingViewController: UITableViewController {
    
    
    
    
    private lazy var dataSoure : [Int : [String]] = {
        return [0:["账号管理","账号安全"],1:["通知","隐私","通用设置"],2:["清理缓存","意见反馈","关于微博"],3:["退出当前账号"]]
    }()
    
    private lazy var nologinDatasoure : [Int : [String]] = {
        return [0:["通用设置"],1:["关于微博"]]
    }()
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = BASECOLOR
        tableView.backgroundColor = BASECOLOR
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        navigationItem.title = "设置"
        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cellId")
        tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier: "bottomCellId")
    }
    
    //MARK: -
    
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        if GYDUserModel.sharedInstance.isLogin {
           return dataSoure.count
        }else {
            return nologinDatasoure.count
        }
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if GYDUserModel.sharedInstance.isLogin {
            return (dataSoure[section]?.count)!
        }else {
            return (nologinDatasoure[section]?.count)!
        }
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        if GYDUserModel.sharedInstance.isLogin {
            if indexPath.section < dataSoure.count - 1 {
                let cell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath)
                cell.textLabel?.text = dataSoure[indexPath.section]![indexPath.row]
                cell.textLabel?.textColor = RGB(40, g: 40, b: 40)
                
                var lineView = cell.contentView.viewWithTag(900)
                if lineView == nil {
                    lineView = UIView()
                    lineView?.tag = 900
                    lineView?.backgroundColor = RGB(230, g: 230, b: 230)
                    cell.contentView.addSubview(lineView!)
                    lineView?.snp_makeConstraints(closure: { (make) in
                        make.left.equalTo(0)
                        make.right.equalTo(0)
                        make.bottom.equalTo(0)
                        make.height.equalTo(0.5)
                    })
                }
                let arrowImageView = cell.viewWithTag(999)
                if arrowImageView == nil {
                    let imageView = UIImageView(image: UIImage(named: "common_icon_arrow"))
                    imageView.tag = 999
                    cell.addSubview(imageView)
                    imageView.frame = CGRect(x: GYDWindowWidth - 12 - 10, y: 16, width: 12, height: 12)
                }
                
                cell.selectionStyle = UITableViewCellSelectionStyle.None
                
                return cell
            }else if indexPath.section == dataSoure.count - 1 {
                let bottomCell = tableView.dequeueReusableCellWithIdentifier("bottomCellId", forIndexPath: indexPath)
                let label = bottomCell.contentView.viewWithTag(500)
                if label == nil {
                    let label1 = UILabel()
                    label1.tag = 500
                    bottomCell.contentView.addSubview(label1)
                    label1.snp_makeConstraints(closure: { (make) in
                        make.left.right.bottom.top.equalTo(0)
                    })
                    label1.text = dataSoure[indexPath.section]![indexPath.row]
                    label1.textColor = ORGINCOLOR
                    label1.textAlignment = NSTextAlignment.Center
                    label1.font = UIFont.boldSystemFontOfSize(17)
                }
                bottomCell.selectionStyle = UITableViewCellSelectionStyle.None
                return bottomCell
            }
        }else {
           
            let cell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath)
            cell.textLabel?.text = nologinDatasoure[indexPath.section]![indexPath.row]
            cell.textLabel?.textColor = RGB(40, g: 40, b: 40)
            
            var lineView = cell.contentView.viewWithTag(900)
            if lineView == nil {
                lineView = UIView()
                lineView?.tag = 900
                lineView?.backgroundColor = RGB(230, g: 230, b: 230)
                cell.contentView.addSubview(lineView!)
                lineView?.snp_makeConstraints(closure: { (make) in
                    make.left.equalTo(0)
                    make.right.equalTo(0)
                    make.bottom.equalTo(0)
                    make.height.equalTo(0.5)
                })
            }
            let arrowImageView = cell.viewWithTag(999)
            if arrowImageView == nil {
                let imageView = UIImageView(image: UIImage(named: "common_icon_arrow"))
                imageView.tag = 999
                cell.addSubview(imageView)
                imageView.frame = CGRect(x: GYDWindowWidth - 12 - 10, y: 16, width: 12, height: 12)
            }
            
            cell.selectionStyle = UITableViewCellSelectionStyle.None
            
            return cell
            
        }
        
        
        return UITableViewCell()
    }
    
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 15
    }
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 1
    }
    
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.section == dataSoure.count - 1  && GYDUserModel.sharedInstance.isLogin == true {
            let userModel = GYDUserModel.sharedInstance
            userModel.loginResultSignal.subscribeNext { (x) in
                if x as! String == LOGINOUT_SUCCESS {
                    self.navigationController?.popViewControllerAnimated(true)
                }
            }
            userModel.loginOut() 
        }
    }
}
